<template>
  <section class="login">
    <!-- <div class="login-full" /> -->
    <div class="login-header">
      <div class="title">{{settings.website.title}}</div>
      <div class="desc">{{settings.website.description}}</div>
    </div>
    <div class="login-img">
      <!-- <img :src="loginLeft" /> -->
    </div>
    <div class="login-con">
      <Alert v-if="error_title||error_content" type="error">
        {{error_title}}
        <span slot="desc">{{error_content}}</span>
      </Alert>
      <h1>用户登录</h1>
      <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="onSubmit">
        <FormItem prop="username">
          <Input :class="{entered:form.username}" v-model="form.username" clearable>
          <common-icon slot="prefix" type="action-iconfont action-account" />
          <span class="placeholder" slot="prefix">账号</span>
          </Input>
        </FormItem>
        <FormItem prop="password">
          <Input :class="{entered:form.password}" v-model="form.password" type="password"
            clearable>
          <common-icon slot="prefix" type="action-iconfont action-password" />
          <span class="placeholder" slot="prefix">密码</span>
          </Input>
        </FormItem>
        <FormItem>
          <Button :loading="loading" @click="onSubmit" long>登录</Button>
        </FormItem>
        <span>测试账号/密码：admin/123456</span>
      </Form>
    </div>
    <div class="login-footer">© {{settings.company_sort}} 2019-2020</div>
  </section>
</template>

<script>
import settings from '@/settings';
import md5 from 'js-md5';
import { mapActions } from 'vuex';
settings.merge({ defaults: { username: '', password: '' } });
export default {
  data() {
    return {
      loading: false,
      settings,
      form: {
        username: settings.defaults.username,
        password: settings.defaults.password
      },
      error_title: '',
      error_content: ''
    };
  },
  computed: {
    rules() {
      return {
        username: [{ required: true, message: '账号未填写', trigger: 'blur' }],
        password: [{ required: true, message: '密码未填写', trigger: 'blur' }]
      };
    }
  },
  methods: {
    ...mapActions(['accountLogin']),
    onSubmit() {
      this.$refs.loginForm.validate(valid => {
        valid && this.submit();
      });
    },
    submit() {
      let params = {
        ...this.form
      };
      params.password = md5(params.password);
      this.loading = true;
      this.accountLogin(params)
        .then(({ data }) => {
          const { redirect } = this.$route.query;
          this.$store.dispatch('login', data);
          if (redirect) {
            this.$router.push({ name: redirect });
            // window.location = `${redirect}?token=${data.token}`;
          } else {
            this.$store.dispatch('gotoHome');
          }
        })
        .catch(error => {
          this.error_title = error.title;
          this.error_content = error.desc;
        })
        .finally((this.loading = false));
    }
  }
};
</script>

<style lang="less">
@theme-color: #fff;
@title-color: #555;
@desc-color: #888;
.login {
  // background-color: @theme-color;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh);
  &-full {
    // background: url("../img/login-background.jpg") no-repeat center fixed;
    background-size: cover;
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
  }
  &-header {
    position: absolute;
    top: calc(5vh);
    left: calc(5vh);
    .title {
      font-size: 34px;
      line-height: 48px;
      color: @title-color;
      font-weight: 500;
      letter-spacing: 1px;
    }
    .desc {
      font-size: 18px;
      line-height: 28px;
      color: @desc-color;
      margin: 10px 0 5px;
    }
  }
  &-con {
    align-self: center;
    margin-left: calc(400px + 200px);
    padding: 32px;
    border-radius: 6px;
    background: @theme-color;
    box-shadow: 0 1px 6px rgba(255, 255, 255, 0.2);
    width: 400px;
    height: 500px;
    display: flex;
    flex-direction: column;
    & h1 {
      color: @title-color;
      font-size: 22px;
      font-weight: 500;
      text-align: left;
      margin: 20px 0;
      letter-spacing: 1px;
    }
  }
  &-tip {
    font-size: 10px;
    text-align: center;
    color: @desc-color;
  }
  &-img {
    align-self: center;
    // margin: 0 auto 0 15%;
    position: absolute;
    margin-left: -300px;
    width: 600px;
    height: 600px;
  }
  &-footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 40px;
    color: @title-color;
    text-align: center;
    background: darken(@theme-color, 10%);
    font-size: 14px;
    line-height: 40px;
  }
  .ivu-form-item-error {
    .ivu-icon,
    .smp-icon {
      color: #ed4014;
    }
    .ivu-form-item-error-tip {
      font-size: 12px;
    }
  }
  .ivu-input {
    font-size: 20px;
    font-weight: 500;
    height: 60px;
    background-color: @theme-color;
    transition-timing-function: ease-in;
    transition-duration: 0.125s;
    &-with-prefix {
      padding-left: 50px;
    }
    &::-webkit-input-placeholder {
      font-weight: 400;
      font-size: 18px;
      line-height: 40px;
      color: @desc-color;
    }
    &-icon-clear {
      line-height: 60px;
      font-size: 24px;
      right: 8px;
      color: @desc-color;
    }
    &-prefix {
      left: 0;
      top: 0;
      width: 50px;

      & > i {
        line-height: 40px;
        font-size: 28px !important;
        color: @desc-color;
        position: absolute;
        left: 12px;
        top: 10px;
      }
      & > svg {
        width: 28px !important;
        height: 28px !important;
        position: absolute;
        left: 12px;
        top: 15px;
      }

      .placeholder {
        position: absolute;
        white-space: nowrap;
        top: 18px;
        left: 50px;
        color: @desc-color;
        font-size: 17px;
        font-weight: 400;
        padding: 0;
        z-index: 1;
        transition-timing-function: ease-in;
        transition-duration: 0.125s;
      }
    }
  }
  .ivu-btn {
    height: 42px;
    background-color: #0070c9;
    background: linear-gradient(#42a1ec, #0070c9);
    border: 1px solid #07c;
    border-radius: 4px;
    color: #fff;
    font-size: 17px;
    line-height: 1.47059;
    font-weight: 400;
    cursor: pointer;
    &:focus {
      box-shadow: none;
    }
  }
  .ivu-alert {
    border-color: rgba(222, 7, 28, 0.2);
    background-color: #fef0f0;
  }
  .entered .ivu-input,
  .ivu-input:focus {
    padding-top: 25px;
    padding-bottom: 1px;
  }
  .entered .ivu-input + .ivu-input-prefix,
  .ivu-input:focus + .ivu-input-prefix {
    .placeholder {
      font-size: 12px;
      top: 10px;
    }
  }
}
</style>
